import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import javax.swing.JButton;
import javax.swing.BoxLayout;


/**
 * HealthHistoryChart
 * Patient Health History - for a given patient plot their weight over time. (JFreeChart is nice for this)
 * 
 * @author mattcooper
 *
 * @version $Revision: 1.0 $
 */


public class HealthHistoryChart extends JPanel {

	/**
	 * Field weights.
	 */
	private double[] weights;
	
	/**
	 * Field dates.
	 */
	private String[] dates;
	
	/**
	 * Field database.
	 */
	private Database database;
	
	/**
	 * Field ID.
	 */
	private String ID;
	
	/**
	 * Field main.
	 */
	private MainWindow main;
	
	/**
	 * Field closeBtn.
	 */
	JButton closeBtn;
	
	/*
	 */
    /**
	 * Constructor for HealthHistoryChart.
	 * @param main MainWindow
	 * @param ID String
	 * @param database Database
	 */
	public HealthHistoryChart(MainWindow main, String ID, Database database) {
        //super("A plot of your weight over time");
		setLayout(null);
		this.main = main;
       	this.database = database;
       	this.ID = ID;
    	this.weights = getTheWeights();
       	this.dates = getTheDates();
       	setup();
    }
    /**
     * Sets up chart
     */
    private void setup(){
        final CategoryDataset dataset = createDataset(weights, dates);
        final JFreeChart chart = createChart(dataset);
        final JPanel chartPanel = new ChartPanel(chart);
        chartPanel.setBounds(10, 0, 750, 406);
        chartPanel.setBackground(Color.LIGHT_GRAY);
        this.setPreferredSize(new Dimension(876, 528));
        setLayout(null);
        this.add(chartPanel);
        chartPanel.setLayout(new BoxLayout(chartPanel, BoxLayout.X_AXIS));
        
        ButtonListener bL = new ButtonListener();
        closeBtn = new JButton("Close");
        closeBtn.setBounds(671, 480, 89, 23);
        closeBtn.addActionListener(bL);
        add(closeBtn);
    }
    
    
     /**
     * Uses the database and the patient ID passed into the constructor in order to make a call to the database
     * @return The double array of weights */
    private double[] getTheWeights() {
    	double[] weight = null;
    	try {
			weight = database.getUserWeightForPlot(ID);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		double[] testWeight = weight;
		return testWeight;
	}

    /**
     * This uses the Database and the ID of the patient provided to the constructor to get the String array of dates
     * @return The string of weights */
	private String[] getTheDates() {
		String[] date = null;
		try{
			date = database.getUserDateForPlot(ID);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			return date;
		}

	/**
     * Creates the dataset for the array of weights / times
     * @param weight double[]
	 * @param dates String[]
	 * @return The dataset. */
    private CategoryDataset createDataset(double[] weight, String[] dates) {
    
        final String series1 = "Weight";

        // create the dataset...
        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        
        for (int x = 0; x < dates.length; x++){
        	dataset.addValue(weight[x], series1, dates[x]);
        }

        return dataset;
                
    }
    
    /**
     * Creates the chart
     * @param dataset  a dataset.
     * @return The chart. */
    private JFreeChart createChart(final CategoryDataset dataset) {
        
        // create the chart...
        final JFreeChart chart = ChartFactory.createLineChart(
            "Weight Over Time",       // chart title
            "Dates",                    // domain axis label
            "Weight",                   // range axis label
            dataset,                   // data
            PlotOrientation.VERTICAL,  // orientation
            true,                      // include legend
            true,                      // tooltips
            false                      // urls
        );

        chart.setBackgroundPaint(Color.white);

        final CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setRangeGridlinePaint(Color.white);

        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        rangeAxis.setAutoRangeIncludesZero(true);
        

        //final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
        return chart;
    }
    
    /**
     * Closes the tab
     */
    private void close(){
    	main.removeTab(this);
    }
    
    /**
     * Actionlistener class to listen for button actions
     * @author matt cooper
     * @version 1.0
     */
    private class ButtonListener implements ActionListener{

		/**
		 * Method actionPerformed.
		 * @param e ActionEvent
		 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
		 */
		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource().equals(closeBtn)){
				close();
			}
			
		}
    	
    }
}